/**
 * 
 */
package com.sebscape.sebcms.slideshow.domain;

import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

/**
 * @author Stephen
 *
 */
public class GenericLinkBean {
	private Long id;
	private String linkText;
	private String linkHref;
	private Map<String,String> attributes;
	
	public GenericLinkBean(Long id, String linkText, String linkHref,
			Map<String,String> attributes) {
		super();
		this.id = id;
		this.linkText = linkText;
		this.linkHref = linkHref;
		this.attributes = attributes;
	}
	
	public GenericLinkBean( String linkText, String linkHref ) {
		super();
		this.linkText = linkText;
		this.linkHref = linkHref;
	}
	
	public String getAsHtml(){
		StringBuffer linkHtml = new StringBuffer( "<a href=\" " + linkHref + " \" ");
		if ( attributes != null ) {
			for (Entry<String, String> attribute : attributes.entrySet()) {
				linkHtml.append(attribute.getKey())
						.append("=\"")
						.append( attribute.getValue() )
						.append("\" ");
			}
		}
		linkHtml.append( ">" ).append( linkText ).append( "</a>" );
		return linkHtml.toString();
	}
	
	public void addAttribute( String attributeName, String attributeValue){
		if( attributeName == null ){
			return; // must provide an attributeName.
		}
		if( attributeName.equalsIgnoreCase( "href" ) ){
			this.linkHref = attributeValue;
			return; // must provide an attributeName.
		}
		if( attributeValue == null ){
			attributeValue = ""; // null is same as empty String.
		}
		// init the attributes Map if needed.
		if( this.attributes == null ){
			this.attributes = new HashMap<String,String>();
		}
		// do not allow duplicates.
		if( this.attributes.containsKey( attributeName ) ){
			this.attributes.remove( attributeName );
		}
		// now add the attribute and value.
		this.attributes.put( attributeName, attributeValue );
	}

	/////////////////////////////////////
	//  Getters and Setters
	////////////////////////////////////

	/**
	 * @return the id
	 */
	public Long getId() {
		return id;
	}

	/**
	 * @param id the id to set
	 */
	public void setId(Long id) {
		this.id = id;
	}

	/**
	 * @return the linkText
	 */
	public String getLinkText() {
		return linkText;
	}

	/**
	 * @param linkText the linkText to set
	 */
	public void setLinkText(String linkText) {
		this.linkText = linkText;
	}

	/**
	 * @return the linkHref
	 */
	public String getLinkHref() {
		return linkHref;
	}

	/**
	 * @param linkHref the linkHref to set
	 */
	public void setLinkHref(String linkHref) {
		this.linkHref = linkHref;
	}

	/**
	 * @return the attributes
	 */
	public Map<String, String> getAttributes() {
		return attributes;
	}

	/**
	 * @param attributes the attributes to set
	 */
	public void setAttributes(Map<String, String> attributes) {
		this.attributes = attributes;
	}
	
}
